teamDistributionMap <- function(teamName,season,clarity,restrictedArea) {
teamdf <- read.csv(paste0("datasets/rosters/",teamName,season,".csv"), stringsAsFactors = FALSE)
temp = 1
for (player in teamdf$PLAYER){
if (temp == 1){
name <- str_replace_all(player, fixed(" "), "")
playerdf <- read.csv(paste0("datasets/players/",name,".csv"), stringsAsFactors = FALSE)
seasonStart = paste0(substring(season, 1, 4) ,"1011")
seasonEnd = paste0("20",substring(season, 6, 7) ,"0430")
playerdf <- filter(playerdf, GAME_DATE < as.numeric(seasonEnd) & GAME_DATE > seasonStart)
result <- playerdf
temp = temp + 1
}
else {
name <- str_replace_all(player, fixed(" "), "")
playerdf <- read.csv(paste0("datasets/players/",name,".csv"), stringsAsFactors = FALSE)
seasonStart = paste0(substring(season,1,4) ,"1011")
seasonEnd = paste0("20",substring(season,6,7) ,"0430")
playerdf <- filter(playerdf, GAME_DATE < as.numeric(seasonEnd) & GAME_DATE > seasonStart)
temp2 <- playerdf
result <- rbind(result,temp2)
}
}
shotDataf <- filter(result,SHOT_ZONE_AREA != "Back Court(BC)")
if (!restrictedArea) {
shotDataf <- filter(shotDataf,SHOT_ZONE_BASIC != "Restricted Area")
}
shotDataf$LOC_X <- as.numeric(as.character(shotDataf$LOC_X))
shotDataf$LOC_Y <- as.numeric(as.character(shotDataf$LOC_Y))
xList <- c()
yList <- c()
count <- c()
name <- c()
for ( x in seq(-260,260,by=clarity)) {
for (y in seq(-60,420,by=clarity)) {
for (player in unique(shotDataf$PLAYER_NAME)) {
shotsinRegion <- filter(shotDataf, PLAYER_NAME == player, LOC_X >= x-25 & LOC_X <= x+25 & LOC_Y >= y-25 & LOC_Y <= y+25)
xList<- c(xList,mean(shotsinRegion$LOC_X))
yList<- c(yList,mean(shotsinRegion$LOC_Y))
name <- c(name, player)
count <- c(count,nrow(shotsinRegion))
}
}
}
bubble <- data.frame(xList,yList,count,name)
bubble
p <- ggplot(bubble, aes(x = xList, y = yList)) +
geom_point(aes(color = name, size = count, alpha=count)) +
scale_size(range = c(.1, 8))
# Adjust the range of points size
#trans="log"
ggplotly(p)
}
teamDistributionMap("LAL","2018-19",25,FALSE)